import java.util.Arrays;

public class BinarySearch {
    //有序数组的二分查找
    public static int binarySearch(int[] array,int key){
        int left = 0;
        int right = array.length - 1;
        while(left <= right){
            int mid = (left + right) / 2;
            if(array[mid] < key){
                left = mid +1;
            }else if (array[mid] > key){
                right = mid -1;
            }else{
                return mid;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        int[] array = {23,34,38,45,67,89,97};
        //利用自己写的方法实现二分查找
        System.out.println(binarySearch(array, 45));
        //利用Arrays中的binarySearch方法实现二分查找
        System.out.println(Arrays.binarySearch(array,97));
    }
}
